﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Jbe.TestSuite.Infrastructure.Interface;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Interop;
using System.Windows;
using System.Windows.Media.Imaging;
using Jbe.Composite.Extensions;
using System.Windows.Controls.Primitives;

namespace Jbe.TestSuite.Infrastructure.Module.Presentation
{
    internal class WpfCreationService : IUIElementCreationService
    {
        public IMenuItem CreateMenuItem()
        {
            return new WpfMenuItem();
        }

        public IMenuItem CreateMenuItem(string text)
        {
            return new WpfMenuItem() { Text = text };
        }

        public IMenuItem CreateMenuItem(string text, System.Drawing.Image image)
        {
            return new WpfMenuItem() { Text = text, Image = image };
        }

        public IToolBar CreateToolBar()
        {
            return new WpfToolBar();
        }

        public IToolBar CreateToolBar(params IToolBarItem[] items)
        {
            return new WpfToolBar(items);
        }

        public IToolBarItem CreateToolBarItem()
        {
            return new WpfToolBarItem();
        }

        public IToolBarItem CreateToolBarItem(string text)
        {
            return new WpfToolBarItem() { Text = text };
        }

        public IToolBarItem CreateToolBarItem(string text, System.Drawing.Image image)
        {
            return new WpfToolBarItem() { Text = text, Image = image };
        }


        public IToolBarItem CreateToolBarComboItem()
        {
            return new WpfToolBarComboItem();
        }

        public IToolBarItem CreateToolBarComboItem(string text)
        {
            return new WpfToolBarComboItem() { Text = text };
        }

        public IToolBarItem CreateToolBarComboItem(string text, System.Drawing.Image image)
        {
            return new WpfToolBarComboItem() { Text = text};
        }


        public IToolBarItem CreateToolBarToggleItem()
        {
            return new WpfToolBarToggleItem();
        }

        public IToolBarItem CreateToolBarToggleItem(string text)
        {
            return new WpfToolBarToggleItem() { Text = text };
        }

        public IToolBarItem CreateToolBarToggleItem(string text, System.Drawing.Image image)
        {
            return new WpfToolBarToggleItem() { Text = text };
        }
    }

    
    // TODO: This class creates Binding errors (see VS Output Window) - The RegionManager is responsible for this.
    internal sealed class WpfMenuItem : MenuItem, IMenuItem
    {
        private EventHandler clickHandlers;
        private System.Drawing.Image image;


        event EventHandler IMenuItem.Click
        {
            add { clickHandlers += value; }
            remove { clickHandlers -= value; }
        }


        public string Text
        {
            get { return Header as string; }
            set { Header = value; }
        }

        public System.Drawing.Image Image
        {
            get { return image; }
            set 
            {
                image = value;

                Image wpfImage = new Image() { Width = 16, Height = 16 };
                wpfImage.Source = ResourceService.CreateBitmapSource(new System.Drawing.Bitmap(image));
                
                Icon = wpfImage; 
            }
        }


        protected override void OnClick()
        {
            base.OnClick();
            if (clickHandlers != null) { clickHandlers(this, EventArgs.Empty); }
        }
    }


    internal sealed class WpfToolBarItem : Button, IToolBarItem
    {
        private EventHandler clickHandlers;
        private System.Drawing.Image image;


        static WpfToolBarItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WpfToolBarItem), new FrameworkPropertyMetadata(ToolBar.ButtonStyleKey));
        }


        event EventHandler IToolBarItem.Click
        {
            add { clickHandlers += value; }
            remove { clickHandlers -= value; }
        }


        public string Text
        {
            get { return ToolTip as string; }
            set { ToolTip = value; }
        }

        public System.Drawing.Image Image
        {
            get { return image; }
            set 
            { 
                image = value;

                Image wpfImage = new Image() { Width = 16, Height = 16 };
                wpfImage.Source = ResourceService.CreateBitmapSource(new System.Drawing.Bitmap(image));

                Content = wpfImage;
            }
        }


        protected override void OnClick()
        {
            base.OnClick();
            if (clickHandlers != null) { clickHandlers(this, EventArgs.Empty); }
        }
    }


    internal sealed class WpfToolBarComboItem : ComboBox, IToolBarItem
    {
        private EventHandler clickHandlers;
        private System.Drawing.Image image;


        static WpfToolBarComboItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WpfToolBarComboItem), new FrameworkPropertyMetadata(ToolBar.ButtonStyleKey));
        }


        event EventHandler IToolBarItem.Click
        {
            add { clickHandlers += value; }
            remove { clickHandlers -= value; }
        }


        public string Text
        {
            get { return ToolTip as string; }
            set { ToolTip = value; }
        }

        public System.Drawing.Image Image
        {
            get { return image; }
            set
            {
                image = value;

                Image wpfImage = new Image() { Width = 16, Height = 16 };
                wpfImage.Source = ResourceService.CreateBitmapSource(new System.Drawing.Bitmap(image));

                //Content = wpfImage;
            }
        }

        /*
        protected override void OnClick()
        {
            base.OnClick();
            if (clickHandlers != null) { clickHandlers(this, EventArgs.Empty); }
        }
        */
    }



    internal sealed class WpfToolBarToggleItem : ToggleButton, IToolBarItem
    {
        private EventHandler clickHandlers;
        private System.Drawing.Image image;


        static WpfToolBarToggleItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WpfToolBarToggleItem), new FrameworkPropertyMetadata(ToolBar.ButtonStyleKey));
        }


        event EventHandler IToolBarItem.Click
        {
            add { clickHandlers += value; }
            remove { clickHandlers -= value; }
        }


        public string Text
        {
            get { return ToolTip as string; }
            set { ToolTip = value; }
        }

        public System.Drawing.Image Image
        {
            get { return image; }
            set
            {
                image = value;

                Image wpfImage = new Image() { Width = 16, Height = 16 };
                wpfImage.Source = ResourceService.CreateBitmapSource(new System.Drawing.Bitmap(image));

                Content = wpfImage;
            }
        }


        protected override void OnClick()
        {
            base.OnClick();
            if (clickHandlers != null) { clickHandlers(this, EventArgs.Empty); }
        }
    }


    internal sealed class WpfToolBar : ToolBar, IToolBar
    {
        IList<IToolBarItem> IToolBar.Items
        {
            get { return new ReadOnlyCollection<IToolBarItem>(Items.OfType<IToolBarItem>().ToList()); }
        }


        public WpfToolBar() : this(null) { }
        
        public WpfToolBar(params IToolBarItem[] items) 
        {
            foreach (IToolBarItem item in items)
            {
                Items.Add(item);
            }
        }
    }
}
